import { Component } from '@angular/core';
import { NavController ,ItemSliding } from 'ionic-angular';
import { GESTURE_PRIORITY_SLIDING_ITEM } from 'ionic-angular/umd/gestures/gesture-controller';
import { Task } from './task';
import { HttpDataProvider } from '../../providers/http-data/http-data';


@Component({
  selector: 'page-tasklist',
  templateUrl: 'tasklist.html'
})
export class TaskListPage {
  tasks: Task[]=[];
  constructor(public navCtrl: NavController,
    public httpData: HttpDataProvider) {
        
  }

 ionViewDidLoad(){
  //  this.tasks=[
  //    {title:'牛奶',status:'open'},
  //    {title:'鸡蛋',status:'open'},
  //    {title:'果汁',status:'open'},
  //    {title:'煎饼',status:'open'}
  //  ];
  this.httpData.getTaskList().subscribe(tasks=>this.tasks=tasks);
  //数据从远端获取变成到云端获取
 }
 /**
  * 新增待办事项
  * @param ItemSliding
  * @param task
  * */ 
  addTask(){
    let theNewTask : string = prompt("新任务");
    if(theNewTask !=""){
        this.tasks.push({title:theNewTask,status:'open'});
    }

  }
/**
  * 标记待办事项
  * @param ItemSliding
  * @param task
  * */ 
 markAsDone(ItemSliding,task:Task){
    task.status = 'done';
    ItemSliding.close();
    
  }
  /**
  * 待办事项
  * @param ItemSliding
  * @param task
  * */ 
 
  removeTask(ItemSliding,task:Task){
   task.status = 'removed';
   let index = this.tasks.indexOf(task);
   if(index > -1 ){
       this.tasks.splice ( index ,1 );
   } 
  }
}
